package com.together.sfipp.controller;

import com.together.sfipp.pojo.Result;
import com.together.sfipp.pojo.User;
import com.together.sfipp.service.UserService;
import com.together.sfipp.utils.JwtUtil;
import com.together.sfipp.utils.Md5Util;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    //注册
    @PostMapping("/register")
    public Result register(@Valid User user) {
        User u = userService.findByUserCard(user.getIdCard());
        if (u == null) {
            //没被占用
            userService.register(user);
            return Result.success();
        }
        else{
            return Result.error("该用户已存在");
        }
    }

    //登录
    @PostMapping("/login")
    public Result<String> login(User user) {
        User u = userService.findByUserCard(user.getIdCard());
        if (u == null) {
            return Result.error("用户名错误");
        }

        //判断密码是否正确
        if(Md5Util.getMD5String(user.getPassword()).equals(u.getPassword())){
            Map<String,Object> claims = new HashMap<>();
            claims.put("idCard",u.getIdCard());
            claims.put("name",u.getUserId());
            claims.put("role",u.isRole());
            claims.put("studnet",u.isStudent());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }

        return Result.error("密码错误");
    }

    //增加信息
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        User u = userService.findByUserCard(user.getIdCard());
        if(u ==null){
            userService.insert(user);
            return Result.success("添加成功");
        }
        else{
            return Result.error("该用户已存在，添加失败");
        }
    }

    //删除信息
    @DeleteMapping("/delete")
    public Result delete(Integer userId) {
        userService.delete(userId);
        return Result.success("删除成功");
    }

    //更新信息
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success("更新成功");
    }

    //查询所有信息
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        List<User> u = userService.selectAll();
        return Result.success(u);
    }

    //查询所需信息
    @GetMapping("/selectNeed")
    public Result<List<User>> selectNeed(User user) {
        List<User> u = userService.selectAll(user);
        return Result.success(u);
    }

    //获取登录用户的信息
    @GetMapping("/info")
    public Result<User> info(@RequestHeader(name = "Authorization")  String token) {
        Map<String,Object> map = JwtUtil.parseToken(token);
        String idCard = (String) map.get("idCard");
        User user = userService.findByUserCard(idCard);
        return Result.success(user);
    }
}
